package cn.ffcs.uec.outStaff.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.WrongValueException;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Messagebox;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.uec.common.util.NodeVoUtil;
import cn.ffcs.uec.common.util.UecConstants;
import cn.ffcs.uec.organization.manager.UecOrgOrgsManager;
import cn.ffcs.uec.organization.model.UecOrgOrgs;
import cn.ffcs.uec.outStaff.action.bean.UecOutStaffAssEditBean;
import cn.ffcs.uec.outStaff.model.UecOutStaffInfo;
import cn.ffcs.uec.staff.model.UecAss;
import cn.ffcs.uom.common.constants.BaseUnitConstants;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.DateUtil;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.PlatformUtil;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.util.StrUtil;

@Controller
@Scope("prototype")
public class UecOutStaffAssEditComposer extends BasePortletComposer {

	private static final long serialVersionUID = 3086340593438520480L;
	/**
	 * 页面bean
	 */
	private UecOutStaffAssEditBean bean = new UecOutStaffAssEditBean();
	/**
	 * 操作类型
	 */
	private String opType;
	/**
	 * 人员信息
	 */
	private UecOutStaffInfo uecOutStaffInfo;
	/**
	 * 考核信息
	 */
	private UecAss uecAss;
	/**
	 * 组织
	 */
	private UecOrgOrgs uecOrgOrgs;

	private UecOrgOrgsManager uecOrgOrgsManager = (UecOrgOrgsManager) ApplicationContextUtil
			.getBean("uecOrgOrgsManager");

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		Components.wireVariables(comp, bean);

	}

	/**
	 * 初始化窗口
	 * 
	 * @throws ParseException
	 */
	public void onCreate$uecAssEditWindow() throws Exception {
		this.bindCombobox();
		this.bindBean();
	}

	/**
	 * 绑定combobox
	 */
	private void bindCombobox() {
		// 考核类型 glbdef1
		ListboxUtils.rendererForEdit(bean.getGlbdef1(),
				NodeVoUtil.getDictionaries(UecConstants.ASSESSMENT_TYPE));
		
		// 考核期间类型glbdef5
		ListboxUtils
				.rendererForEdit(bean.getGlbdef5(), NodeVoUtil
						.getDictionaries(UecConstants.ASSESSMENT_TYPE));
		// 绩效等级--pkPerank
		ListboxUtils.rendererForEdit(bean.getPkPerank(),
				NodeVoUtil.getDictionaries(UecConstants.PERFORMANCE_GRADE));
	}

	/**
	 * bindBean
	 * 
	 * @throws Exception
	 * 
	 * @throws ParseException
	 */
	public void bindBean() throws Exception {
		opType = (String) arg.get("opType");
		if ("add".equals(opType)) {
			this.bean.getUecAssEditWindow().setTitle("新增人员考核信息");
			bean.getUecAssEditDiv().setHeight("30%");
			uecOutStaffInfo = (UecOutStaffInfo) arg.get("uecOutStaffInfo");
		}
		if ("view".equals(opType)) {
			this.bean.getUecAssEditWindow().setTitle("查看人员考核信息");
			uecAss = (UecAss) arg.get("uecAss");
			uecOutStaffInfo = (UecOutStaffInfo) arg.get("uecOutStaffInfo");
			if (null != uecAss) {
				PubUtil.fillBeanFromPo(uecAss, bean);
				bean.getPkOrgass().setValue(
						uecOrgOrgsManager
								.getUecOrgOrgsNameByPkOrg(uecOutStaffInfo
										.getPkHrorg()));
				stringTransToDate();
				bean.getButtonDiv().setVisible(false);
				PubUtil.disableComponent(bean.getUecAssEditWindow(), true);
			}
		}
		if ("edit".equals(opType)) {
			this.bean.getUecAssEditWindow().setTitle("编辑人员考核信息");
			bean.getUecAssEditDiv().setHeight("30%");
			uecAss = (UecAss) arg.get("uecAss");
			uecOutStaffInfo = (UecOutStaffInfo) arg.get("uecOutStaffInfo");
			if (null != uecAss) {
				PubUtil.fillBeanFromPo(uecAss, bean);
				if(!StrUtil.isNullOrEmpty(uecAss.getPkOrgass())) {
				bean.getPkOrgass().setValue(
						uecOrgOrgsManager
								.getUecOrgOrgsNameByPkOrg(uecAss.getPkOrgass()));
				}
				if(!StrUtil.isNullOrEmpty(uecAss.getPkOrg())) {
					bean.getPkOrg().setValue(
							uecOrgOrgsManager
									.getUecOrgOrgsNameByPkOrg(uecAss.getPkOrg()));
					}
				stringTransToDate();
			}
		}
	}

	/**
	 * 保存
	 * 
	 * @throws Exception
	 */
	public void onOk() throws Exception {
		String msg = this.checkUacStaffData();
		if (null != msg) {
			Messagebox.show(msg);
			return;
		}
		String currentName = PlatformUtil.getCurrentUser().getScreenName();
		if ("add".equals(opType)) {
			uecAss = UecAss.newInstance();
			PubUtil.fillPoFromBean(bean, uecAss);
			dateTransToString();
			uecAss.setStaffId(uecOutStaffInfo.getOutStaffId());
			uecAss.setNmStaffType(BaseUnitConstants.NM_OUT_STAFF_TYPE);
			uecAss.setPkPsndoc(uecOutStaffInfo.getPkPsnMsg());
			
			//考核组织
			if(!StrUtil.isNullOrEmpty(bean.getPkOrgass().getValue())){
				// 组织主键
				String pkorg = (String) bean.getPkOrgass().getOrganization().getOrg();
				uecAss.setPkOrgass(pkorg);
			}
			//所属组织
			if(!StrUtil.isNullOrEmpty(bean.getPkOrg().getValue())){
				// 组织主键
				String pkorg = (String) bean.getPkOrg().getOrganization().getOrg();
				uecAss.setPkOrg(pkorg);
			}
			
			// 创建人
			uecAss.setCreator(currentName);
			// 创建时间
			uecAss.setCreationtime(DateUtil.getCurrenDate());
			uecAss.addOnly();
			Events.postEvent("onOK", this.self, uecAss);
			this.onCancel();
			Messagebox.show("新增成功！");
		}
		if ("edit".equals(opType)) {
			//PubUtil.fillPoFromEditorBean(bean, uecAss);
			String orgsName = uecOrgOrgsManager.getUecOrgOrgsNameByPkOrg(uecAss.getPkOrgass());
			String pkOrgNameOld = uecOrgOrgsManager.getUecOrgOrgsNameByPkOrg(uecAss.getPkOrg());
			PubUtil.fillPoFromEditorBean(uecAss, bean);
			dateTransToString();
			//判断组织是否有变化
			String pkorgName = bean.getPkOrgass().getValue();
			String pkorgNameNew = bean.getPkOrg().getValue();
			if(!pkorgName.equals(orgsName)){
				//考核组织
				if(!StrUtil.isNullOrEmpty(bean.getPkOrgass().getValue())){
					// 组织主键
					if(!StrUtil.isNullOrEmpty(bean.getPkOrgass().getOrganization().getOrg())) {
						String pkorg = (String) bean.getPkOrgass().getOrganization().getOrg();
						uecAss.setPkOrgass(pkorg);
					}
				}
			}else {
				uecAss.setPkOrgass(orgsName);
			}
			if(!pkOrgNameOld.equals(pkorgNameNew)){
				//所属组织
				if(!StrUtil.isNullOrEmpty(pkorgNameNew)){
					// 组织主键
					if(!StrUtil.isNullOrEmpty(bean.getPkOrg().getOrganization().getOrg())) {
						String pkorg = (String) bean.getPkOrg().getOrganization().getOrg();
						uecAss.setPkOrg(pkorg);
					}
				}
			}else {
				uecAss.setPkOrg(pkOrgNameOld);
			}
			
			
			// 修改人
			uecAss.setModifier(currentName);
			// 修改时间
			uecAss.setModifiedtime(DateUtil.getCurrenDate());
			uecAss.updateOnly();
			Events.postEvent("onOK", this.self, uecAss);
			this.onCancel();
			Messagebox.show("更新成功！");
		}
	}

	/**
	 * 取消
	 */
	public void onCancel() {
		// 关闭窗口
		this.bean.getUecAssEditWindow().onClose();
	}

	/**
	 * 检测填入的参数是否为空
	 */
	private String checkUacStaffData() {
		if (StrUtil.isNullOrEmpty(bean.getGlbdef1().getSelectedItem()
				.getValue())) {
			return "请选择考核期间类型";
		} 
		//else if (StrUtil.isNullOrEmpty(bean.getSchemename().getValue())) {
		//	return "请填写考核方案名称";
		//}
		 else if (StrUtil.isNullOrEmpty(bean.getBegindate())) {
				return "请选择起始日期";
			}
		 else if (StrUtil.isNullOrEmpty(bean.getEnddate())) {
			 return "请选择终止日期";
		 }
	    else if (StrUtil.isNullOrEmpty(bean.getPkPerank().getSelectedItem()
				.getValue())) {
			return "请选择绩效等级";
		} /*else if (StrUtil.isNullOrEmpty(bean.getSchemescore().getValue())) {
			return "请填写考核成绩";
		}*/
		return null;
	}

	/**
	 * 将Date类型时间转成String类型 Date --> String 一套模型，两套标准：日期类型需要格式化
	 */
	public void dateTransToString() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		if (!StrUtil.isNullOrEmpty(bean.getBegindate().getValue())) {
			// 起始日期（干部任免）
			uecAss.setGlbdef2(formatter.format(bean.getBegindate().getValue()));
			uecAss.setBegindate(formatter.format(bean.getBegindate().getValue()));
		}
		
		if(!StrUtil.isNullOrEmpty(bean.getEnddate().getValue())){
			//结束日期
			uecAss.setEnddate(formatter.format(bean.getEnddate().getValue()));
		}

	}

	/**
	 * 将String类型的时间转换成Date类型
	 * 
	 * @throws WrongValueException
	 * 
	 * @throws ParseException
	 */
	public void stringTransToDate() throws Exception {
		// 时间类型转换：String --> Date
		// 起始日期（干部任免）
		// SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
		if (null != uecAss.getBegindate()) {
			/*String glbdef2 = uecAss.getGlbdef2();
			if (glbdef2.length() == 7) {
				glbdef2 = glbdef2 + ".01";
			}
			if (glbdef2.length() == 4) {
				glbdef2 = glbdef2 + ".01.01";
			}*/
			bean.getBegindate().setValue(DateUtil.patternStrToDate(uecAss.getBegindate()));
		}
		
		if(null != uecAss.getEnddate()){
			bean.getEnddate().setValue(DateUtil.patternStrToDate(uecAss.getEnddate()));
		}
	}

}
